package com.my.yyr.controller.safty;

import com.my.yyr.base.Result;
import com.my.yyr.dto.UserDto;
import com.my.yyr.exception.BusinessException;
import com.my.yyr.exception.SysException;
import com.my.yyr.model.Role;
import com.my.yyr.service.safty.UserService;
import org.hibernate.validator.constraints.CodePointLength;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/safty/user")
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("")
    public Map<String,Object> getUserPage(UserDto userDto){
       return userService.getUserPage(userDto);
    }

    @PostMapping("")
    public Result execAddUser(@RequestBody UserDto userDto){
        try {
            userService.addUser(userDto);
            return Result.success("新增用户成功！");
        } catch (BusinessException e) {
            return Result.fail(e.getMessage());
        } catch (SysException e) {
            return Result.fail("系统故障！");
        }
    }

    @DeleteMapping("")
    public Result execDelUser(@RequestBody String[] ids){
        try {
            userService.deleteUsers(ids);
            Result result = Result.success("删除用户成功！");
            System.out.println(result);
            return result;
        } catch (BusinessException e) {
            return Result.fail(e.getMessage());
        } catch (SysException e) {
            return Result.fail("系统故障！");
        }
    }

    @DeleteMapping("/{id}")
    public Result execDelUser(@PathVariable String id){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            userService.deleteUsers(id);
            return Result.success("删除用户成功！");
        } catch (BusinessException e) {
            return Result.fail(e.getMessage());
        } catch (SysException e) {
            return Result.fail("系统故障！");
        }
    }

    @GetMapping("/{userId}")
    public Result existsUserId(@PathVariable String userId){
        boolean exist = userService.existsUserId(userId);
        if(exist){
            return Result.fail("账号已存在！");
        }else{
            return Result.success();
        }

    }

    @PutMapping("")
    public Result execUpdUser(@RequestBody UserDto userDto){
        try {
            userService.updUser(userDto);
            return Result.success("修改用户成功！");
        } catch (BusinessException e) {
            return Result.fail(e.getMessage());
        } catch (SysException e) {
            return Result.fail("系统故障！");
        }
    }

    @PutMapping("/{uid}/{opr}")
    public Result execStatusChange(@PathVariable String uid, @PathVariable String opr){
        if(userService.updateStatus(uid,opr)){
            return Result.success("用户状态已成功变更！");
        }
        return Result.success("用户状态变更失败！");
    }

    @GetMapping("/role")
    public List<Role> getRoleList(){
        return userService.getRoleList();
    }
    @GetMapping("/roleIds/{uid}")
    public List<String> getUserRoleIdList(@PathVariable String uid){
        return userService.getUserRoleIdList(uid);
    }

    @PutMapping("/role")
    public Result confirmRoles(@RequestBody UserDto userDto){
        userService.confirmRoles(userDto);
        return Result.success("角色分配成功！");
    }

}
